﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace YmtAuth.LogService
{
    /// <summary>
    /// 用户登录历史表（不包含第三方平台用户登录）
    /// </summary>
    public class UserLoginLogHistory : LogEntityBase
    {
        public override string LogType
        {
            get
            {
                return "UserLoginLogHistory";
            }
        }
        
        /// <summary>
        /// 请求ID
        /// </summary>
        public string RequestId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get; set; }
        
        /// <summary>
        /// 登录ID
        /// </summary>
        public string LoginId { get; set; }
        
        /// <summary>
        /// 登录IP
        /// </summary>
        public string LoginIp { get; set; }
        
        /// <summary>
        /// 客户端应用ID（userservice.ymatou.com, etc）或应用分组（App、pc、MainSite, etc）
        /// </summary>
        public string ClientType { get; set; }

        /// <summary>
        /// 终端类型（Android, Ios, Pc, etc）
        /// </summary>
        public string TerminalType { get; set; }

        /// <summary>
        /// 设备号ID（IMEI, IDFA）
        /// </summary>
        public string DeviceId { get; set; }

        /// <summary>
        /// 设备号的标识（由个推服务器产生（通过app上的个推SDK获取），用于向设备推送消息）
        /// </summary>
        public string ClientId { get; set; }

        /// <summary>
        /// 验证时间
        /// </summary>
        public virtual DateTime AddTime { get { return DateTime.Now; } }
        
        /// <summary>
        /// 验证耗时
        /// </summary>
        public double RunTime { get; set; }

        /// <summary>
        /// 安全认证类型
        /// </summary>
        public string AuthType { get; set; }

        private string ip;
        /// <summary>
        /// 服务端Ip
        /// </summary>
        public string ServerIp
        {
            get
            {
                if (string.IsNullOrEmpty(ip))
                    ip = LocalIp();
                return ip;
            }
        }

        /// <summary>
        /// 安全认证结果code
        /// </summary>
        public int ResponseCode { get; set; }

        /// <summary>
        /// 安全认证结果描述
        /// </summary>
        public string Desc { get; set; }

        public string LocalIp()
        {
           var ips=  Dns.GetHostEntry(Dns.GetHostName())
                .AddressList.FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork);
            return ips == null ? null : ips.ToString();
        }
    }
    public class TokenAuthLog : AuthSafeVerifyLog
    {
        public override string LogType
        {
            get
            {
                return "YmatouUserTokenAuthLog";
            }
        }

    }
    public class ThirdPartyUserLoginLog : AuthSafeVerifyLog
    {
        public override string LogType
        {
            get
            {
                return "YmatouThirdPartyUserLoginLog";
            }
        }

    }
    public class UserLockLog : LogEntityBase
    {
        public const string KefuLockedUser = "客服锁定用户";
        public const string KefuUnLockedUser = "客服解除用户锁定";
        public const string KefuLockedUserIp = "客服锁定用户Ip";
        public const string KefuUnLockedUserIp = "客服解除用户IP锁定";
        public const string BlackListIp = "请求TOKEN认证IP是黑名单";
        public const string LoginIpBlackList = "登录Ip为黑名单";

        public string LoginId { get; set; }
        public string LoginIp { get; set; }
        public int UserId { get; set; }
        public DateTime LoginTime { get; set; }
        public string Operator { get; set; }
        public string Description { get; set; }
        /// <summary>
        /// 1:Login IP黑名单，2：请求token IP 黑名单，3：客服锁定用户,4：客服解除用户锁定，5：客服锁定用户IP，6：客服解除用户IP锁定
        /// </summary>
        public string OpType { get; set; }
        public string AppSource { get; set; }
        public override string LogType
        {
            get { return "UserLockedLog"; }
        }
    }
}
